package com.cloudcc.mobile.view.attendance;

import android.view.View;
import butterknife.Bind;

import com.cloudcc.cloudframe.adapter.CommonAdapter;
import com.cloudcc.cloudframe.ui.viewgroup.loadmore.LoadMoreContainer;
import com.cloudcc.cloudframe.ui.viewgroup.refresh.PtrFrameLayout;
import com.cloudcc.cloudframe.util.ListUtils;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.adapter.AttendanceListAdapter;
import com.cloudcc.mobile.entity.AttendanceEntity;
import com.cloudcc.mobile.event.AttendanceEventList.AttendanceListEvent;
import com.cloudcc.mobile.presenter.AttendancePresenter;
import com.cloudcc.mobile.view.main.fragment.BaseListFragment;
import com.cloudcc.mobile.widget.CloudCCTitleBar;
import com.cloudcc.mobile.widget.CloudCCTitleBar.OnTitleBarClickListener;
import com.cloudcc.mobile.widget.listview.CloudCCListView;
import com.cloudcc.mobile.widget.listview.CloudCCListView.OnRefreshOrLoadMoreListener;

/**
 * 事件任务
 * 
 * @Description: TODO()
 * @author wangzhiqiang
 * @date 2016-3-29 上午9:14:22
 * @param <T>
 */
public class AttendanceListFragment extends BaseListFragment implements
		OnRefreshOrLoadMoreListener, OnTitleBarClickListener {

	@Bind(R.id.headerbar)
	CloudCCTitleBar headerbar;
	@Bind(R.id.listview1)
	CloudCCListView mListView;
	protected CommonAdapter<AttendanceEntity> mAdapter;
	private AttendancePresenter mAttendancePresenter = new AttendancePresenter();

	@Override
	public void init() {
		super.init();
		headerbar.setOnTitleBarClickListener(this);
		mListView.setOnRefreshOrLoadMoreListener(this);
		mListView.requestRefresh();
		headerbar.setImage();
	}

	@Override
	public void requestData() {
		headerbar.beginProgress();
		mAttendancePresenter.queryHistory(currentPage);
	}

	public void onEventMainThread(AttendanceListEvent event) {
		headerbar.completeProgress();
		boolean shoudClear = mListView.refreshLoadMoreUI(
				ListUtils.isEmpty(event.getData()), event.isError(),
				isFromRefresh(), event.getMessage());
		if (mAdapter == null) {
			mAdapter = new AttendanceListAdapter(mContext);
			mListView.setAdapter(mAdapter);
		}
		if (shoudClear) {
			mAdapter.changeData(event.getData());
		} else {
			mAdapter.addData(event.getData());
		}
	}

	@Override
	public void onRefresh(PtrFrameLayout frame) {
		onRefreshData();
	}

	@Override
	public void onLoadMore(LoadMoreContainer frame) {
		onLoadMoreData();
	}

	@Override
	public int getLayoutId() {
		return R.layout.attendance_list_frag;
	}

	@Override
	protected boolean hasEventLife() {
		return true;
	}

	@Override
	public void onClickLeft(View v) {
		getActivity().finish();
	}

	@Override
	public void onClickRight(View v) {
		// TODO Auto-generated method stub

	}

}
